Skip to content

fix(local): find the SQLite DB via db-path.php so local push --with-db pushes it - #18

Open
masumiwp wants to merge 2 commits into
mainfrom
fix/sqlite-db-path
Open

masumiwp wants to merge 2 commits into
mainfrom
fix/sqlite-db-path

Conversation

@masumiwp

@masumiwp masumiwp commented Dec 3, 2026 •

Copy link
Copy Markdown

Bug

instawp local push <name> --with-db pushes files, then skips the DB:
✗ No local database found (expected wp-content/database/.ht.sqlite). Skipping DB push.
Reported on Windows 11; it happens on every OS.

Root cause

Playground downloads the latest sqlite-database-integration (3.1.3). Its WP_SQLite_Storage keeps the DB at wp-content/database/.ht.<bin2hex(random_bytes(16))>/.ht.sqlite and records the path in wp-content/database/db-path.php (return __DIR__ . '/.ht.<hex>/.ht.sqlite';). On boot it migrates a legacy .ht.sqlite into that directory. pushDatabase() and the isClone check looked only at the legacy .ht.sqlite.

Fix

resolveLocalSqlitePath(instance) (src/lib/local-sqlite.ts) is pure fs: no logging, no exit. It checks, in order:

  1. db-path.php: the __DIR__ . '/…' literal is parsed with the same regex as instacp #852 and never executed.
  2. .ht.*/.ht.sqlite: if several exist, the newest by mtime wins and a warning lists them all.
  3. Legacy .ht.sqlite, then .ht.sqlite.php (the plugin migrates both).

Every candidate is realpath'd, so symlinks are followed. It must be a non-empty regular file inside wp-content/database; containment uses path.relative, which is Windows-safe.

Call sites:

  • pushDatabase() → resolvePushSqlitePath(): prints warnings and the chosen DB path before the confirm prompt. When nothing is found, the error lists every path checked. It refuses (even with --force) when db-path.php exists but its target is missing, outside the dir or unparseable: WP_SQLite_Storage treats db-path.php as the authority, so any fallback is a DB WordPress is not using, and pushing it would overwrite the cloud with stale data. A lone .ht.*/ match (no db-path.php) is pushed with a warning.
  • buildServerArgs(), isClone: uses the resolver.
  • Clone import: no code change. It already wipes wp-content/database (removing any db-path.php and old secret dir) before writing .ht.sqlite, and the plugin then migrates it. A comment now explains why that wipe is load-bearing: the plugin reads the recorded path first and ignores a legacy file while the recorded DB exists.

Deliberate divergence from instacp #852 / the plugin's own lookup. They try legacy before the glob; this follows the ticket's order (glob before legacy). The two only differ when db-path.php is unusable and both a secret dir and a legacy file exist.

Behaviour change for QA

isClone now means "has a persisted DB" at any of the three locations. So a local create instance takes the clone-mode mounts from its second start onward. That is how it behaved before plugin 3.x, when .ht.sqlite existed after the first start.

Tests

src/__tests__/local-sqlite.test.ts (25 tests) covers:

  • resolver: db-path.php beats a stale legacy file and a newer secret dir; glob only; glob before legacy; multi-match by mtime (both orders); unparseable db-path.php; legacy; none found (lists every path); no db dir; db dir is a file; traversal rejected (a real DB exists at the target); symlinked secret dir rejected; symlinked instance path accepted;
  • push refusal (outside / missing recorded file) and the push happy paths;
  • buildServerArgs isClone wiring;
  • isInsideDir under path.win32.

Every mechanism has a mutation that turns a named test red: containment, sort, parser, empty-file check, legacy list, separator check, glob-before-legacy order, dbDir realpath, isClone wiring, refusal, flag set/clear, lone-match warning, readdir guard. Full suite 294/294, tsc clean. Code review (code-reviewer agent): round 1 WARN, no blockers; findings applied in e63ee6b.

ClickUp: https://app.clickup.com/t/14ypaj0z2fb
Precedent: InstaWP/instacp#852
Agentric session: https://aos.agents.instawp.net/#/sessions/aos-ses_981564bda5883aa9

Next step (human): /code-review ultra <this PR> before merge. Do not merge before QA's verdict.

— Engineer (agent-os)

🤖 Generated with Claude Code

InstaWP Engineer (bot) and others added 2 commits December 3, 2026 11:31
…db` pushes it

sqlite-database-integration 3.x (which Playground downloads at latest) moves the
DB to wp-content/database/.ht.<hex>/.ht.sqlite and records it in db-path.php.
pushDatabase() and the isClone check only looked at the legacy .ht.sqlite, so
`local push --with-db` pushed files and then skipped the DB ("No local database
found") on every OS.

Add resolveLocalSqlitePath(): db-path.php (parsed, never executed; must resolve
inside wp-content/database) -> .ht.*/.ht.sqlite (newest mtime, warns if several)
-> legacy .ht.sqlite. Use it in pushDatabase() and buildServerArgs()' isClone.
The clone import already wipes wp-content/database before writing .ht.sqlite;
documented why that is load-bearing (a leftover db-path.php would win).

ClickUp: https://app.clickup.com/t/14ypaj0z2fb

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ew fixes

- resolvePushSqlitePath(): WP_SQLite_Storage treats db-path.php as the authority,
  so when it exists but its target is missing/outside/unparseable every fallback
  is a DB WordPress is not using. local push now refuses instead of overwriting
  the cloud with it (even with --force).
- Warn on a lone .ht.*/ match too (WordPress never scans for these dirs).
- readdir/stat failures are 'not found', not a throw (isClone runs on every start).
- Tests: glob-before-legacy order, symlinked instance path, push refusal cases,
  buildServerArgs isClone wiring (exported for the test), db dir is a file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@masumiwp

masumiwp commented Dec 3, 2026

Copy link
Copy Markdown
Author

Ready for QA at e63ee6b. Code review (code-reviewer agent): round 1 WARN, round 2 PASS. Branch is frozen while QA (tsk_a2b451b455fe3e83) runs; please don't merge before its verdict.
src/lib/local-env.ts:194 — behaviour change: isClone now means "a DB exists", so local create instances use clone-mode mounts from their second start onward (as they did before plugin 3.x). QA is checking persistence.
Open nits, not fixing: pushDatabase→resolvePushSqlitePath wiring has no test; refusal message doesn't name the reason (the warning above it does).
Human: /code-review ultra 18 before merge.

@masumiwp

masumiwp commented Dec 3, 2026

Copy link
Copy Markdown
Author

QA: FAIL ❌: the DB-path fix works. Criterion 4 fails on a local create site: from the 2nd start, files outside 5 mounted dirs are lost.
Env: Linux host, PR build node dist/index.js (Playground via npx, PHP 8.3), throwaway sandbox 2743019 (team 163411) · InstaWP/cli #18 @e63ee6b · unit tests 294/294 · Windows not tested
Premise ✅: a fresh instance writes database/db-path.php → .ht.<32hex>/.ht.sqlite. A clone's legacy .ht.sqlite is migrated there on first boot.

# Result
1 ✅ clone → edit title → push --with-db --force printed Local database: …/.ht.2a540a4e…/.ht.sqlite, pushed 12 tables, and the cloud page <title> is LOCAL-EDIT-18
2 ✅ DB dir removed: exit 1, lists db-path.php, .ht.*/.ht.sqlite, .ht.sqlite, .ht.sqlite.php
3 ✅ db-path→.ht.gone: exit 1 with --force. The cloud DB is unchanged (a local C3-MUST-NOT-ARRIVE title did not arrive). ⚠ nit: files are rsynced before the refusal (marker reached the cloud), yet the message says "nothing was pushed". It also prints two "falling back / using …" warnings first.
4 clone ✅ edited title, 2 posts and Hello Dolly active survive restarts. Exactly one .ht.<hex>/
4 create ❌ posts and plugin state survive, one .ht.<hex>/. But clone-mode mounts only database/plugins/themes/uploads/mu-plugins. Files that session 1 wrote to wp-content/fonts/ and languages/ are still on the host, but from session 2 they are not mounted and WP can't see them (Font Library fonts, language packs). Uploads written in session 2+ vanish on stop when uploads/ didn't exist at start: a local create site that uploaded nothing in session 1 loses every later media file while its attachment rows persist. On base, a 3.x DB never matched isClone, so local create always mounted the whole wp-content
5 ✅ clone --force → new .ht.1b8dde…/, cloud title CLOUD-V2-fresh

Broke at: local create → start #1 → stop → start #2 → write wp-content/{fonts,languages,uploads}/x → stop → start #3: glob() returns [] for all three, and nothing is on the host.
Env workaround (not the PR): in the PPU SSH jail wp db export fails (mariadb-dump missing, no /run/mysqld socket), and the --with-db backup path ~/… doesn't exist in the chroot. To get past it I set DB_HOST=127.0.0.1, linked mysqldump as ~/.local/bin/mariadb-dump, and used --no-backup after a manual export. As shipped, customers hit these failures on local clone and push --with-db too. — QA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant